﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Game.DataAccess.DAO;
using Game.DataAccess.DTO;
using Game.Utilities;
using Game.Utilities.Config;
using Game.Utilities.Log;

namespace Game.DataAccess.DAOImpl
{
    public class SlotDaoImpl : ISlotDao
    {
       


        /// <summary>
        ///-212 : Tiền đặt không hợp lệ
        ///-50Tài khoản không tồn tại
        ///-51Tài khoản không đủ Sao
        ///- 99 Lỗi hệ thống
        /// </summary>
        /// <param name="serviceid"></param>
        /// <param name="accessToken"></param>
        /// <param name="accountId"></param>
        /// <param name="username"></param>
        /// <param name="betRoom"></param>
        /// <param name="betValue"></param>
        /// <param name="balance"></param>
        /// <param name="referenceId"></param>
        /// <param name="clientIp"></param>
        /// <param name="descriptions"></param>
        /// <param name="responseStatus"></param>
        public void SlotMachine_Create(int serviceid, string accessToken, int accountId, string username, int betRoom, int betValue,
                                       ref long balance, long referenceId, string clientIp, string descriptions,
                                       ref long responseStatus)
        {
            //referenceId chính là betlogid bàn đầu insertbetlog
            try
            {

                var db = new DBHelper(Config.VTCDBConnectionString);
                var pars = new SqlParameter[11];
                pars[0] = new SqlParameter("@_ServiceID", serviceid);
                pars[1] = new SqlParameter("@_AccessToken", accessToken);
                pars[2] = new SqlParameter("@_AccountID", accountId);
                pars[3] = new SqlParameter("@_UserName", username);
                pars[4] = new SqlParameter("@_BetRoom", betRoom);
                pars[5] = new SqlParameter("@_BetValue", betValue);
                pars[6] = new SqlParameter("@_Balance", SqlDbType.BigInt) { Direction = ParameterDirection.Output };
                pars[7] = new SqlParameter("@_ReferenceID", referenceId);
                pars[8] = new SqlParameter("@_ClientIP", clientIp);
                pars[9] = new SqlParameter("@_Descriptions", descriptions);
                pars[10] = new SqlParameter("@_ResponseStatus", SqlDbType.Int) { Direction = ParameterDirection.Output };
                db.ExecuteNonQuerySP("SP_SlotMachine_Create", pars);
                long.TryParse(pars[6].Value.ToString(), out balance);
                long.TryParse(pars[10].Value.ToString(), out responseStatus);
            }
            catch (Exception ex)
            {
                NLogManager.Warning("SlotMachine_Create :" + ex.ToString());
                
            }

           
            
        }

        /// <summary>
        /// --62 : Quỹ thưởng không đủ để trả
        /// </summary>
        /// <param name="serviceid"></param>
        /// <param name="accessToken"></param>
        /// <param name="accountId"></param>
        /// <param name="username"></param>
        /// <param name="betRoom"></param>
        /// <param name="prizeValue"></param>
        /// <param name="balance"></param>
        /// <param name="referenceId"></param>
        /// <param name="clientIp"></param>
        /// <param name="descriptions"></param>
        /// <param name="responseStatus"></param>
       
        public void SlotMachine_Award(int serviceid, string accessToken, int accountId, string username, int betRoom, int prizeValue,
                                      ref long balance, long referenceId, string clientIp, string descriptions, ref long responseStatus)
        {
            try
            {

            var db = new DBHelper(Config.VTCDBConnectionString);
            var pars = new SqlParameter[11];
            pars[0] = new SqlParameter("@_ServiceID", serviceid);
            pars[1] = new SqlParameter("@_AccessToken", accessToken);
            pars[2] = new SqlParameter("@_AccountID", accountId);
            pars[3] = new SqlParameter("@_UserName", username);
            pars[4] = new SqlParameter("@_BetRoom", betRoom);
            pars[5] = new SqlParameter("@_PrizeValue", prizeValue);
            pars[6] = new SqlParameter("@_Balance", SqlDbType.BigInt) { Direction = ParameterDirection.Output };
            pars[7] = new SqlParameter("@_ReferenceID", referenceId);
            pars[8] = new SqlParameter("@_ClientIP", clientIp);
            pars[9] = new SqlParameter("@_Descriptions", descriptions);
            pars[10] = new SqlParameter("@_ResponseStatus", SqlDbType.Int) { Direction = ParameterDirection.Output };
            db.ExecuteNonQuerySP("SP_SlotMachine_Create", pars);

            long.TryParse(pars[6].Value.ToString(), out balance);
            long.TryParse(pars[10].Value.ToString(), out responseStatus);

            }
            catch (Exception ex)
            {
                NLogManager.Warning("SlotMachine_Award:" + ex.ToString());

            }


        }

        public void Account_Insert(int acountId, string acountName, int pointWin, int pointPayment)
        {
            try
            {

            var db = new DBHelper(Config.GameVietDBConnectionString);
            var pars = new SqlParameter[4];
            pars[0] = new SqlParameter("@_AccountID", acountId);
            pars[1] = new SqlParameter("@_AccountName", acountName);
            pars[2] = new SqlParameter("@_PointWin", pointWin);
            pars[3] = new SqlParameter("@_PointPayment", pointPayment);
            db.ExecuteNonQuerySP("Account_Insert", pars);
         
            }

            catch (Exception ex)
            {
                NLogManager.Warning("GetAccountInfo:" + ex);
                 
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="accountId"></param>
        /// <returns></returns>
        public List<AccountInfo> GetAccountInfo(int accountId)
        {
            try
            {

            
            var db = new DBHelper(Config.GameVietDBConnectionString);
            var pars = new SqlParameter[1];
            pars[0] = new SqlParameter("@AccountID", accountId);
               
            List<AccountInfo> dt = db.GetListSP<AccountInfo>("GetAccountInfo", pars);
            return dt;
            }
            catch (Exception ex)
            {
                NLogManager.Warning("GetAccountInfo:" + ex.ToString());
                return new List<AccountInfo>();
            }

        }

       

        /// <summary>
        /// 
        /// </summary>
        /// <param name="accountId"></param>
        /// <returns></returns>
        public List<AccountHistory> GetHistoryWinner(int accountId)
        {
            try
            {

           
            var db = new DBHelper(Config.GameVietDBConnectionString);
            var pars = new SqlParameter[1];
            pars[0] = new SqlParameter("@AccountID", accountId);

            List<AccountHistory> dt = db.GetListSP<AccountHistory>("GetHistoryWinner", pars);
            return dt;
            }
            catch (Exception ex)
            {
                NLogManager.Warning("GetHistoryWinner:" + ex.ToString());
                return new  List<AccountHistory>();
            }
        }

        public List<HistoryJackport> GetHistoryJackport()
        {
            try
            {
            var db = new DBHelper(Config.GameVietDBConnectionString);
            var pars = new SqlParameter[0];

            List<HistoryJackport> dt = db.GetListSP<HistoryJackport>("GetHistoryJackport", pars);
            return dt;

            }
            catch (Exception ex)
            {
                NLogManager.Warning("GetHistoryJackport:" + ex.ToString());
                return new List<HistoryJackport>();
            }
        }

        public List<JackpotInfo> GetJackpotInfo()
        {
            try
            {
                var db = new DBHelper(Config.GameVietDBConnectionString);
                var pars = new SqlParameter[0];

                List<JackpotInfo> dt = db.GetListSP<JackpotInfo>("GetJackpotInfo", pars);
                return dt;
            }
            catch (Exception ex)
            {
                NLogManager.Warning("GetJackpotInfo:" + ex.ToString());
                return new List<JackpotInfo>();
            }
        }

        public List<DigGoldInfo> GetDigGoldInfo(long betLogId, int accountId)
        {
            try
            {
                var db = new DBHelper(Config.GameVietDBConnectionString);
                var pars = new SqlParameter[2];
                pars[0] = new SqlParameter("@BetLogID", betLogId);
                pars[1] = new SqlParameter("@AccountID", accountId);
                List<DigGoldInfo> dt = db.GetListSP<DigGoldInfo>("GetDigGoldInfo", pars);
                return dt;
            }
            catch (Exception ex)
            {
                NLogManager.Warning("GetDigGoldInfo:" + ex.ToString());
                return new List<DigGoldInfo>();
            }
        }

        public List<WinnerInfo> GetWinnerInfo(long betLogId,ref int isFree, ref int sumBetMoney, ref int sumWinMoney,ref int winMoneyDigGold, ref int countLineWin,
                                  ref string bonnusMiniGame, ref string resultString, ref  int response)
        {

            try
            {
    

                //betLogId lấy từ insertBetlog 
                var db = new DBHelper(Config.GameVietDBConnectionString);
                var pars = new SqlParameter[9];
                pars[0] = new SqlParameter("@BetLogID", betLogId);
                pars[1] = new SqlParameter("@IsFree", SqlDbType.Bit) { Direction = ParameterDirection.Output };
               
                pars[2] = new SqlParameter("@SUMBetMoney", SqlDbType.Int) {Direction = ParameterDirection.Output};
                pars[3] = new SqlParameter("@SUMWinMoney", SqlDbType.Int) {Direction = ParameterDirection.Output};
                pars[4] = new SqlParameter("@WinMoneyDigGold", SqlDbType.Int) {Direction = ParameterDirection.Output};


                pars[5] = new SqlParameter("@CountLineWin", SqlDbType.Int) {Direction = ParameterDirection.Output};
                pars[6] = new SqlParameter("@BonnusMiniGame", SqlDbType.NVarChar, 250)
                    {
                        Direction = ParameterDirection.Output
                    };
                pars[7] = new SqlParameter("@ResultString", SqlDbType.NVarChar, 50)
                    {
                        Direction = ParameterDirection.Output
                    };
                pars[8] = new SqlParameter("@Response", SqlDbType.Int) {Direction = ParameterDirection.Output};
                var dt = db.GetListSP<WinnerInfo>("GetWinnerInfo", pars);
                int.TryParse(pars[1].Value.ToString(), out isFree);
                int.TryParse(pars[2].Value.ToString(), out sumBetMoney);
                int.TryParse(pars[3].Value.ToString(), out sumWinMoney);
                int.TryParse(pars[4].Value.ToString(), out winMoneyDigGold);
                int.TryParse(pars[5].Value.ToString(), out countLineWin);
                bonnusMiniGame = pars[6].Value.ToString();
                resultString = pars[7].Value.ToString();
                int.TryParse(pars[8].Value.ToString(), out response);
                return dt;

            }
            catch (Exception ex)
            {
                NLogManager.Warning("GetWinnerInfo:" + ex.ToString());
                return new List<WinnerInfo>();
            }

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="betLogId"></param>
        /// <param name="resultStringInput"></param>
        /// <param name="isFree"></param>
        /// <param name="sumBetMoney"></param>
        /// <param name="sumWinMoney"></param>
        /// <param name="winMoneyDigGold"></param>
        /// <param name="countLineWin"></param>
        /// <param name="bonnusMiniGame"></param>
        /// <param name="resultString"></param>
        /// <param name="response"></param>
        /// <returns></returns>
        public List<WinnerInfo> GetWinnerInfo_WithResultString(long betLogId, string resultStringInput, ref int isFree, ref int sumBetMoney,
                                                   ref int sumWinMoney, ref int winMoneyDigGold, ref int countLineWin,
                                                   ref string bonnusMiniGame, ref string resultString, ref int response)
        {
            try
            {
     

                //betLogId lấy từ insertBetlog 
                var db = new DBHelper(Config.GameVietDBConnectionString);
                var pars = new SqlParameter[10];
                pars[0] = new SqlParameter("@BetLogID", betLogId);
                pars[1] = new SqlParameter("@ResultStringInput", resultStringInput);
                pars[2] = new SqlParameter("@IsFree", SqlDbType.Bit) { Direction = ParameterDirection.Output };
                
                pars[3] = new SqlParameter("@SUMBetMoney", SqlDbType.Int) { Direction = ParameterDirection.Output };
                pars[4] = new SqlParameter("@SUMWinMoney", SqlDbType.Int) { Direction = ParameterDirection.Output };
                pars[5] = new SqlParameter("@WinMoneyDigGold", SqlDbType.Int) { Direction = ParameterDirection.Output };
                pars[6] = new SqlParameter("@CountLineWin", SqlDbType.Int) { Direction = ParameterDirection.Output };
                pars[7] = new SqlParameter("@BonnusMiniGame", SqlDbType.NVarChar, 250)
                {
                    Direction = ParameterDirection.Output
                };
                pars[8] = new SqlParameter("@ResultString", SqlDbType.NVarChar, 50)
                {
                    Direction = ParameterDirection.Output
                };
                pars[9] = new SqlParameter("@Response", SqlDbType.Int) { Direction = ParameterDirection.Output };
                var dt = db.GetListSP<WinnerInfo>("GetWinnerInfo_WithResultString", pars);
                
                int.TryParse(pars[2].Value.ToString(), out isFree);
                int.TryParse(pars[3].Value.ToString(), out sumBetMoney);
                int.TryParse(pars[4].Value.ToString(), out sumWinMoney);
                int.TryParse(pars[5].Value.ToString(), out winMoneyDigGold);
                int.TryParse(pars[6].Value.ToString(), out countLineWin);
                bonnusMiniGame = pars[7].Value.ToString();
                resultString = pars[8].Value.ToString();
                int.TryParse(pars[9].Value.ToString(), out response);
                return dt;

            }
            catch (Exception ex)
            {
                NLogManager.Warning("GetWinnerInfo_WithResultString:" + ex.ToString());
                return new List<WinnerInfo>();
            }
        }

        public void InsertBetLog(int accountId, int roomId, string betLine, int isFree, string ipAdd, ref int betMoney, ref long responseStatus)
        {
            try
            {

            var db = new DBHelper(Config.GameVietDBConnectionString);
            var pars = new SqlParameter[7];
            pars[0] = new SqlParameter("@AccountId", accountId);
            pars[1] = new SqlParameter("@RoomID", roomId);

            pars[2] = new SqlParameter("@BetLine", betLine);
            pars[3] = new SqlParameter("@IsFree ", isFree);
            pars[4] = new SqlParameter("@IPAdd", ipAdd);
            pars[5] = new SqlParameter("@BetMoney", SqlDbType.Int) { Direction = ParameterDirection.Output };
            pars[6] = new SqlParameter("@Response", SqlDbType.BigInt) { Direction = ParameterDirection.Output };
            
            db.ExecuteNonQuerySP("InsertBetLog", pars);
            int.TryParse(pars[5].Value.ToString(), out betMoney);
            long.TryParse(pars[6].Value.ToString(), out responseStatus);
            }
            catch (Exception ex)
            {
                NLogManager.Warning("InsertBetLog:" + ex.ToString());
            }



        }

        public void InsertChangingLog_Input(long betlogId, int accountId, string accountName, int changeMoney,int moneyBonnus, int processStatus,
                                            string ip, string descryption, int status, ref long requestId)
        {

            //changeMoney số tiền đặt cược
            // moneyBonnus =0;
            //processStatus =0;
            //descryption ="insert trừ tiền + changmoney ( so tien)"
            //status =0;

            try
            {

            

            var db = new DBHelper(Config.GameVietDBConnectionString);
            var pars = new SqlParameter[10];
            
            pars[0] = new SqlParameter("@BetLogID ", betlogId);
            pars[1] = new SqlParameter("@AccountId", accountId);
            pars[2] = new SqlParameter("@AccountName", accountName);

            pars[3] = new SqlParameter("@ChangeMoney", changeMoney);
            pars[4] = new SqlParameter("@MoneyBonnus ", moneyBonnus);
            pars[5] = new SqlParameter("@ProcessStatus", processStatus);
            pars[6] = new SqlParameter("@IP", ip);
            pars[7] = new SqlParameter("@Descryption", descryption);
            pars[8] = new SqlParameter("@Status", status);
            pars[9] = new SqlParameter("@_RequestId", SqlDbType.BigInt) { Direction = ParameterDirection.Output };
            db.ExecuteNonQuerySP("InsertChangingLog_Input", pars);
            long.TryParse(pars[9].Value.ToString(), out requestId);

            }
            catch (Exception ex)
            {
                NLogManager.Warning("InsertChangingLog_Input:" + ex.ToString());
            }
	 
        }

        

        /// <summary>
        /// 
        /// </summary>
        /// <param name="betlogId"></param>
        /// <param name="accountId"></param>
        /// <param name="accountName"></param>
        /// <param name="changeMoney"></param>
        /// <param name="processStatus"></param>
        /// <param name="ip"></param>
        /// <param name="descryption"></param>
        /// <param name="status"></param>
        /// <param name="requestId"></param>
        public void InsertChangingLog_Output(long betlogId,int accountId, string accountName,  ref int changeMoney, int processStatus,
                                             string ip, string descryption, int status, ref long requestId)
        {

            //betlogId lấy từ bước 1
            //processStatus =0;
            //changeMoney = tiền thắng
            // descryption = 'isert api + tien" + changemoney
            //status=0;

            try
            {

            var db = new DBHelper(Config.GameVietDBConnectionString);
            var pars = new SqlParameter[9];
            
            pars[0] = new SqlParameter("@BetLogID ", betlogId);
            pars[1] = new SqlParameter("@AccountId", accountId);
            pars[2] = new SqlParameter("@AccountName", accountName);
            pars[3] = new SqlParameter("@ChangeMoney", SqlDbType.Int) { Direction = ParameterDirection.Output };
          
            pars[4] = new SqlParameter("@ProcessStatus", processStatus);
            pars[5] = new SqlParameter("@IP", ip);
            pars[6] = new SqlParameter("@Descryption", descryption);
            pars[7] = new SqlParameter("@Status", status);
            pars[8] = new SqlParameter("@_RequestId", SqlDbType.BigInt) { Direction = ParameterDirection.Output };

            db.ExecuteNonQuerySP("InsertChangingLog_Output", pars);

            int.TryParse(pars[3].Value.ToString(), out changeMoney);
            long.TryParse(pars[8].Value.ToString(), out requestId);
            }
            catch (Exception ex)
            {
                NLogManager.Warning("InsertChangingLog_Output:" + ex.ToString());
            }


     
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="requestId"></param>
        /// <param name="processStatus"></param>
        /// <param name="reponseStatus"></param>
        /// <param name="descryption"></param>
        /// <param name="status"></param>
        /// <param name="returnvalue"></param>
        public void UpdateChangingLog_Input(long requestId, int processStatus, int reponseStatus, string descryption, int status,
                                            ref int returnvalue)
        {


            //requestId lấy từ bước 3
            //processStatus =1 thành công =-1 thất bại
            //reponseStatus cái bên VTC trả về
            //status  =1

            try
            {

            
             var db = new DBHelper(Config.GameVietDBConnectionString);
            var pars = new SqlParameter[6];
            
            pars[0] = new SqlParameter("@_RequestId ", requestId);
            pars[1] = new SqlParameter("@ProcessStatus", processStatus);
            pars[2] = new SqlParameter("@ReponseStatus", reponseStatus);
            pars[3] = new SqlParameter("@Descryption", descryption);
            pars[4] = new SqlParameter("@Status", status);

      
            pars[5] = new SqlParameter("@_returnvalue", SqlDbType.Int) { Direction = ParameterDirection.Output };

            db.ExecuteNonQuerySP("UpdateChangingLog_Input", pars);

            int.TryParse(pars[5].Value.ToString(), out returnvalue);
            }
            catch (Exception ex)
            {
                NLogManager.Warning("UpdateChangingLog_Input:" + ex.ToString());
            }
 

        }

     
        public void UpdateChangingLog_Output(long requestId, int processStatus, int reponseStatus, string descryption,
                                             int status,
                                             ref int returnvalue)
        {

            //requestId Lấy từ bước 6
            //processStatus= thanh con g= 1, + thất bại =-1
            //status = 1
            //reponseStatus lấy từ bước 7
            // description = "gọi api cộng thành công"
            try
            {

            
            var db = new DBHelper(Config.GameVietDBConnectionString);
            var pars = new SqlParameter[6];

            pars[0] = new SqlParameter("@_RequestId ", requestId);
            pars[1] = new SqlParameter("@ProcessStatus", processStatus);
            pars[2] = new SqlParameter("@ReponseStatus", reponseStatus);
            pars[3] = new SqlParameter("@Descryption", descryption);
            pars[4] = new SqlParameter("@Status", status);

            pars[5] = new SqlParameter("@_returnvalue", SqlDbType.Int) {Direction = ParameterDirection.Output};

            db.ExecuteNonQuerySP("UpdateChangingLog_Input", pars);
            int.TryParse(pars[5].Value.ToString(), out returnvalue);

            }
            catch (Exception ex)
            {
                NLogManager.Warning("UpdateChangingLog_Output:" + ex.ToString());
            }
 

        }




    }
}
